$(document).ready(function () {

    var getUrlParameter = function getUrlParameter(sParam) {
        var sPageURL = decodeURIComponent(window.location.search.substring(1)),
            sURLVariables = sPageURL.split('&'),
            sParameterName,
            i;

        for (i = 0; i < sURLVariables.length; i++) {
            sParameterName = sURLVariables[i].split('=');

            if (sParameterName[0] === sParam) {
                return sParameterName[1] === undefined ? true : sParameterName[1];
            }
        }
    };

    var id = getUrlParameter('id');

    //是否编辑
    var update = getUrlParameter('update');

    console.log(' -- update ---- ' + update);

    items = ['#title', '#inc_name', '#room_name', '#leader_name', '#leader_mobile', '#inc_tel',
        '#inc_email', '#start', '#end', '#allDay', '#post_name', '#recruit_professional',
        '#enrolment_numbers', '#working_place', '#job_requirements', '#working_conditions', '#the_intern',
        '#detail_info'];
    
    function setUpdate(update) {
        if(1 == Number(update)){ //可编辑

        }else{  //不可编辑
            items.forEach(function (element) {
                $(element).prop("readonly", true);
                $(element).attr('readonly', true); //versions older than 1.6
            });
            $( ".btn-primary" ).hide();
        }

    }

    //设置是否可编辑
    setUpdate(update);
    
    //获取申请的各种信息
    function setInputValues(values) {
        items.forEach(function (element) {
            $(element).val(values[element.split('#')[1]]);
            if(element == '#start'){
                var start = moment.unix(values[element.split('#')[1]]);
                $(element).prop('value', start.format("LLL"));

            }
            if(element == '#end'){
                var end = moment.unix(values[element.split('#')[1]]);
                $(element).prop('value', end.format("LLL"));
            }


            if(element == '#allDay'){
                var allDay = values[element.split('#')[1]];

                console.log(' --- allDay --- ' + allDay);
                if(Number(allDay) == 1){
                    $(element).prop('value', '1');
                }else{
                    $(element).prop('value', '2');
                }
            }

            if(element == '#the_intern'){
                var theIntern = values[element.split('#')[1]];
                if(Number(theIntern) == 1){
                    $(element).prop('value', '1');
                }else{
                    $(element).prop('value', '2');
                }
            }
        });

    }


    console.log("id:   ", id);
    $.get('/reservation-query', {id: id}, function (json) {
        if (json.code === 0) {
            // console.log("json: ", json.data);
            setInputValues(json.data);
        }
    });


    var startTime = $('#datetimepicker1');
    var endTime = $('#datetimepicker2');
    endTime.datetimepicker({
        // locale: 'zh-cn',
        // date: new Date(1434544882775)
    });

    startTime.datetimepicker({
        // locale: 'zh-cn',
        // Default: false
    });

    //修改预约
    $('.btn-primary').click(function () {

        var data = getDataJquery();
        data['room_id'] = roomId;
        data['id'] = numberId;
        console.log('data:', data);

        $.post('/reservation-add', toSerialize(data), function (json) {
            console.log("return json: ",json);
            if (json.code === 0) {
                console.log("申请成功！");

                $(".btn-primary").notify("申请成功！", "success");
            }
        });
    });
});